﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIGEB.Prototipo.Control;
using SIGEB.Prototipo.Vista.UI;
using bibliotecaObservador;
using System.Collections;
using System.Resources;

namespace SIGEB.Prototipo
{
    public partial class frm_Base : Form, IObservador
    {
        #region CONSTRUCTORES

        public frm_Base()
        {
            InitializeComponent();
            erp_error = new ErrorProvider();
            erp_error.Icon = Properties.Resources.errorp;
            erp_error.BlinkStyle = ErrorBlinkStyle.NeverBlink;
        }

        public frm_Base(controlPrincipal cp)
        {
            InitializeComponent();
            erp_error = new ErrorProvider();
            erp_error.Icon = Properties.Resources.errorp;
            erp_error.BlinkStyle = ErrorBlinkStyle.NeverBlink;
            asignarUI(this.Controls);
            this.cp = cp;
            cp.registraObservador(this);
        }

        #endregion

        #region MÉTODOS

        protected void asignarUI(System.Windows.Forms.Control.ControlCollection coleccion)
        {
            foreach (System.Windows.Forms.Control c in coleccion)
            {
                if (c.GetType().GetProperty("Font") != null)
                {
                    if (c is FlowLayoutPanel)
                        asignarUI(((FlowLayoutPanel)c).Controls);
                    else if (c is GroupBox)
                        asignarUI(((GroupBox)c).Controls);
                    else if (c is Panel)
                        asignarUI(((Panel)c).Controls);
                    else if (c is TabControl)
                        asignarUI(((TabControl)c).Controls);
                    else if (c is TableLayoutPanel)
                        asignarUI(((TableLayoutPanel)c).Controls);
                    else if (c is DataGridView)
                    {
                        ((DataGridView)c).ColumnHeadersDefaultCellStyle.Font = Fuente.obtenerFuente();
                        ((DataGridView)c).RowsDefaultCellStyle.Font = Fuente.obtenerFuente();
                    }
                    else if (c is DateTimePicker)
                        ((DateTimePicker)c).CalendarFont = Fuente.obtenerFuente();
                    c.Font = Fuente.obtenerFuente();
                }
            }
        }

        #endregion

        #region "OBSERVADOR"

        public virtual void actualizar(object referencia, EventoActualizacion e)
        {
            //throw new Exception("No se puede llamar al metodo actualizar de frm_base");
        }

        #endregion

        #region EVENTOS

        private void btn_Atras_Click(object sender, EventArgs e)
        {
            ocultar();
        }

        #endregion

        #region MANEJADORES DE EVENTOS

        virtual protected void frm_Base_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
        }

        protected bool confirmarCierre()
        {
            return MessageBox.Show(
                "¿Desea cerrar la aplicación?",
                "Confirmación",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) == DialogResult.Yes;
        }

        protected bool confirmarOperacion(String texto)
        {
            if (texto == null)
                texto = "Esta operación cerrará la ventana y no guardará los cambios realizados";
            return MessageBox.Show(
                texto +
                Environment.NewLine + "¿Desea continuar?",
                "Advertencia",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Warning) == DialogResult.OK;
        }

        protected void ventanaError(string mensajeError)
        {
            MessageBox.Show(mensajeError, "Error de datos", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        protected void cargarVentana(object sender, EventArgs e)
        {
            activada = true;
        }

        virtual protected void CerrarVentana(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            ocultar();
        }

        internal void ocultar()
        {
            Hide();
            oculta = true;
        }

        protected void mostrarInfo(string p)
        {
            MessageBox.Show(p);
        }

        protected void setError(System.Windows.Forms.Control cntrl, string error)
        {
            erp_error.SetIconAlignment(cntrl, ErrorIconAlignment.MiddleRight);
            erp_error.SetIconPadding(cntrl, 2);
            erp_error.SetError(cntrl, error);
            if (cntrl is TextBox || cntrl is MaskedTextBox
                && cntrl.GetType().GetProperty("BackColor") != null)
            {
                cntrl.BackColor = Color.LemonChiffon;
            }
        }

        protected void borrarError(System.Windows.Forms.Control cntrl)
        {
            erp_error.SetError(cntrl, string.Empty);
            if (cntrl is TextBox || cntrl is MaskedTextBox
                && cntrl.GetType().GetProperty("BackColor") != null)
            {
                cntrl.BackColor = Color.White;
            }
        }

        internal void hacerVisibleBase(object[] arg)
        {
            if (!activada)
            {
                Show();
            }
            else
            {
                oculta = false;
                Show();
                CenterToScreen();
                BringToFront();
            }
            hacerVisible(arg);
        }

        virtual public void hacerVisible(object[] arg)
        {
        }

        #endregion

        #region ATRIBUTOS

        private System.Drawing.Icon icnAlert;
        //protected ErrorProvider erp_error;
        protected controlPrincipal cp;
        protected ArrayList datosPrueba;
        protected bool oculta;
        internal bool activada;

        #endregion
    }
}
